package com.cxhc.demo1006;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.cxhc.demo1006.imageWatcher.ImageWatcher;
import com.cxhc.demo1006.module.GirlsBean;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseMvpActivity implements MainView, MainAdapter.OnItemClickCallback {

    MainPresenter presenter;
    private RefreshLayout mRefreshLayout;

    private int START_PAGE = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        super.initData();
        presenter = new MainPresenter(this);
        presenter.getGilrs(START_PAGE);
    }

    RecyclerView rv;
//    EasyRecyclerView rv;
    MainAdapter adapter;

    @Override
    protected void initView() {
        super.initView();
        rv = findViewById(R.id.rv);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        imageWatcher = findViewById(R.id.image_watcher);
        initWatcher();
        rv.setLayoutManager(new GridLayoutManager(mContext, 6));
//        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
//                StaggeredGridLayoutManager.VERTICAL);
//        rv.setLayoutManager(staggeredGridLayoutManager);
//        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter = new MainAdapter());
        adapter.setOnItemClickCallback(this);
        //刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                adapter.clear();
                START_PAGE = 1;
                presenter.getGilrs(START_PAGE);
                refreshlayout.finishRefresh();
            }
        });
        //加载更多
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                START_PAGE++;
                presenter.getGilrs(START_PAGE);
                refreshlayout.finishLoadmore();
                Log.e(TAG, "onLoadmore: " + START_PAGE);
            }
        });


//        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    if (mContext != null){
//                        Glide.with(mContext).resumeRequests();//恢复Glide加载图片
//                    }
//                }else {
//                    if (mContext != null){
//                        Glide.with(mContext).pauseRequests();//禁止Glide加载图片
//                    }
//                }
//            }
//        });

    }

    @Override
    public void onGirlsLoaded(GirlsBean girlsBean) {
//        adapter.setNewData(girlsBean);
        adapter.addNewData(girlsBean);
    }

    @Override
    public void onGirlsLoadError() {

    }

    @Override
    public void OnGirlsLoadStart() {

    }

//    @Override
//    public void onItemClick(int pos) {
//        Toast.makeText(mContext,pos+"点击了",Toast.LENGTH_SHORT).show();
//    }

    @Override
    public void onItemClick(int pos, GirlsBean bean, ImageView imageView) {
        List<ImageView> ivlist = new ArrayList<>(bean.getResults().size());
        List<String> urlList = new ArrayList<>(bean.getResults().size());
        ImageView ivg;
        for (int i = 0; i < bean.getResults().size(); i++) {
            if (i == pos) {
                ivg = imageView;
            } else {
                ivg = new ImageView(this);
            }
            ivlist.add(ivg);
            urlList.add(bean.getResults().get(i).getUrl());
//            Log.e(TAG, "onClickPic: " + bean.getResults().get(i).getUrl());
        }
        imageWatcher.show(ivlist.get(pos), ivlist, urlList);
//        Toast.makeText(this,"imagewatcher",Toast.LENGTH_SHORT).show();
    }

    public static final String TAG = MainActivity.class.getSimpleName();
    private ImageWatcher imageWatcher;

    private void initWatcher() {
        imageWatcher.setErrorImageRes(R.mipmap.ic_launcher);  // 配置error图标 如果不介意使用lib自带的图标，并不一定要调用这个API
        imageWatcher.setHintMode(ImageWatcher.TEXT);//设置指示器（默认小白点）
//        imageWatcher.setOnPictureLongPressListener(this); // 长按图片的回调，你可以显示一个框继续提供一些复制，发送等功能
        imageWatcher.setLoader(new ImageWatcher.Loader() {//调用show方法前，请先调用setLoader 给ImageWatcher提供加载图片的实现
            @Override
            public void load(Context context, String url, final ImageWatcher.LoadCallback lc) {
                Glide.with(context).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        lc.onResourceReady(resource);
                    }

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        lc.onLoadStarted(placeholder);
                    }

                    @Override
                    public void onLoadFailed(Drawable errorDrawable) {
                        lc.onLoadFailed(errorDrawable);
                    }
                });
            }
        });
    }

    long clickTime = 0;

    @Override
    public void onBackPressed() {
        if (imageWatcher.getVisibility() == View.VISIBLE) {
            imageWatcher.handleBackPressed();
            return;
        }
        if (System.currentTimeMillis() - clickTime > 2000) {
            Toast.makeText(mContext, "再点击一次退出程序", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "onBackPressed: " + System.currentTimeMillis());
            clickTime = System.currentTimeMillis();
            return;
        }
//        else {
//            finish();
//            System.exit(0);
//        }
        super.onBackPressed();
    }
}
